<select name="category_id" id="category_id">
    <?php foreach ($cats as $cat): ?>
    <?php $parentId = $cat['id']; ?>
    <?php if(!$cat['parent_id']): ?>
    <option<?php echo ($cat['id'] == $_POST['category_id']) ? ' selected' : '';?> value="<?php echo $cat['id']; ?>"><?php echo $cat['name']; ?></option>
    <?php foreach ($cats as $subcat): ?>
        <?php if($subcat['parent_id']==$parentId): ?>
        <option<?php echo ($subcat['id'] == $_POST['category_id']) ? ' selected' : '';?> value="<?php echo $subcat['id']; ?>">----- <?php echo $subcat['name']; ?></option>
        <?php endif; ?>
    <?php endforeach; ?>
    <?php endif; ?>
    <?php endforeach; ?>
</select>